#
# Cookbook Name:: om
# Recipe:: setupmq
# Installs WebSphere and sets up a profile for it.
#
# Copyright 2013, IBM Corporation
#
# All rights reserved - Do Not Redistribute
#

include_recipe "gitclientsetup::default"

wasuser = node[:was][:user]
wasgroup = node[:was][:group]

group "#{node[:was][:group]}" do
  action :create
end

user "#{node[:was][:user]}" do
  gid "#{node[:was][:group]}"
  action :create
end

include_recipe "om::wasservice"
include_recipe "om::customer_overrides"
template "/tmp/configure_was_jvm.jy" do
	source "configure_was_jvm.jy.erb"
	owner node[:was][:user]
	group node[:was][:group]
	variables(
		:yfs_local => node[:om][:yfs_local]
	)
	action :create
end

earParams = "#{node[:common][:buildEarParams]}"
earParams = earParams.split(' ')
warfiles = nil
earParams.each() do |x|
	if(x.index('warfiles')!=nil)
		warfiles = x.split('=')[1]
	end
end
warfiles=warfiles.split(',')
wardeplstr = ''
warfiles.each() do |x|
	wardeplstr=wardeplstr+"[ #{x}.war #{x}.war,WEB-INF/web.xml ' + omsserver + ']"
end
template "/tmp/deploy_ear.jy" do
    source "runt_deployear.jy.erb"
    user node[:was][:user]
    owner node[:was][:user]
    variables(
		:earName => "#{node[:common][:earName]}",
		:warlist => wardeplstr
	)
    action :create
end

include_recipe "was::install"


class Chef::Recipe
include Orchestrate
end

zkep = "#{node[:zookeeper][:ip]}:#{node[:zookeeper][:port]}"
chef_gem 'zk'

startinstr "Install WAS, configure a profile and deploy ear into the application server"

cuser = node[:chef][:user]
cgroup = node[:chef][:group]
deployment = node[:deployment]

bash "Make YFS Local Directory and give permissions to wasuser"  do
    user "root"
    code <<-EOH
        mkdir -p #{node[:om][:yfs_dir]}
        mkdir -p #{node[:om][:yfs_local]}/properties
		chown -R #{node[:was][:user]}:#{node[:was][:group]} #{node[:om][:yfs_dir]}
    EOH
end

bash "Create profile for websphere" do
  user "root"
  creates "#{node[:was]['installDir']}/profiles/AppSrv01"
  cwd node[:was][:installDir]
  code <<-EOH
    #{node[:was][:installDir]}/bin/managesdk.sh -setNewProfileDefault -sdkName 1.7_64
    #{node[:was][:installDir]}/bin/managesdk.sh -setCommandDefault -sdkName 1.7_64
    #{node[:was][:installDir]}/bin/manageprofiles.sh  -create -profileName AppSrv01
  EOH
end


`ps -ef |grep server1 | grep -v grep`
if $? != 0 
  bash "Start WebSphere Server in the new profile" do
    user "root"
    group node[:was][:group]
    cwd node[:was][:installDir]
    code <<-EOH
      #{node[:was][:installDir]}/bin/startServer.sh server1
    EOH
  end
end

bash "Setup memory and generic jvm arguments on server1 and restart AppServer after jvm argument changes." do
  user "root"
  cwd node[:was][:installDir]
  creates "/tmp/jvm.arg.changes.done"
  code <<-EOH
    #{node[:was][:installDir]}/profiles/AppSrv01/bin/wsadmin.sh -lang jython -f /tmp/configure_was_jvm.jy
    #{node[:was][:installDir]}/bin/stopServer.sh server1
    #{node[:was][:installDir]}/bin/startServer.sh server1
    if [ $? -eq  0 ]; then
      touch /tmp/jvm.arg.changes.done
    fi
  EOH
end

ruby_block "Checking for ear build completion in ZooKeeper." do
    block do
        i_url = "/deployments/" + deployment + "/config/om/runtime/application/ear/status"
        dep = Dependency.new(i_url, node[:client][:status], zkep)
        dep.validate!
    end
end

bash "Download EAR from the file server" do
    user cuser
    group cgroup
    cwd "/tmp/"
    code <<-EOH
        scp #{node[:common][:scp_repo]}/generated/#{deployment}/#{node[:common][:earName]} /tmp/#{node[:common][:earName]}
	chmod -R 755 /tmp/runtime.ear
    EOH
end


bash "Set up external properties for EAR"  do
    user node[:was][:user]
    group node[:was][:group]
    code <<-EOH
      cd /tmp
  		unzip -oq /tmp/#{node[:common][:earName]} properties.jar
  		unzip -oq /tmp/properties.jar -d  #{node[:om][:yfs_local]}/properties
      sed -i 'sX#{node[:om][:installDir]}/propertiesX#{node[:om][:yfs_local]}/properties/Xg' #{node[:om][:yfs_local]}/properties/servers.properties
      cp /tmp/customer_overrides.properties #{node[:om][:yfs_local]}/properties
    EOH
end

#include_recipe  "om::unzip_ri"
include_recipe  "om::fetchfilebindings"


ruby_block "Publishing AppServer Status to ZooKeeper." do
    block do
        i_url = "/deployments/" + deployment + "/config/om/runtime/websphere/#{node[:fqdn]}/server/status"
        dep = Dependency.new(i_url, node[:client][:status], zkep)
        dep.publish!
    end
end


bash "Deploy EAR to Application Server." do
	user "root"
	cwd node[:was][:installDir]
	code <<-EOH
		#{node[:was][:installDir]}/profiles/AppSrv01/bin/wsadmin.sh -lang jython -f /tmp/deploy_ear.jy
	EOH
end

ruby_block "Publishing EAR Deployment Completion Status to ZooKeeper." do
    block do
        i_url = "/deployments/" + deployment + "/config/om/runtime/websphere/#{node[:fqdn]}/ear/status"
        dep = Dependency.new(i_url, node[:client][:status], zkep)
        dep.publish!
    end
end

stopinstr "Install WAS, configure a profile and deploy ear into the application server"
